package com.student.Activitys.activity;

import android.content.Intent;

import com.hyphenate.EMContactListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.ui.EaseContactListFragment;
import com.hyphenate.exceptions.HyphenateException;
import com.student.Activitys.R;
import com.student.Activitys.activity.base.BaseActivity;
import com.student.Activitys.ring_letter.activity.ChatActivity;
import com.student.Activitys.utils.LogUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目名称：
 * 类描述：通讯录
 * 创建人：张飞祥
 * 创建时间：2017/9/18 11:15
 * 修改人：张飞祥
 * 修改时间：2017/9/18 11:15
 * 修改备注：
 */
public class MailListActivity extends BaseActivity {

    //联系人
    private static EaseContactListFragment contactListFragment;

    @Override
    protected void initUI() {
        setContentView(R.layout.act_public_add_fragment);
        //联系人初始化
        contactListFragment = new EaseContactListFragment();
        //联系人列表刷新
        ContactInitialization();
    }

    @Override
    protected void initData() {
        //好友列表 数据状态监听
        ContactStatusMonitor();
        //加载 联系人 fragment
        getSupportFragmentManager().beginTransaction().replace(R.id.layout_add_fg, contactListFragment).commit();
        //设置item点击事件
        contactListFragment.setContactListItemClickListener(new EaseContactListFragment.EaseContactListItemClickListener() {
            @Override
            public void onListItemClicked(EaseUser user) {
                startActivity(new Intent(MailListActivity.this, ChatActivity.class).putExtra(EaseConstant.EXTRA_USER_ID, user.getUsername()));
            }
        });
    }

    @Override
    protected void getNotData() {
    }

    /**
     * 环信
     *
     * 当前好友列表 数据状态监听 方法
     */
    private void ContactStatusMonitor() {

        EMClient.getInstance().contactManager().setContactListener(new EMContactListener() {

            @Override
            public void onContactAgreed(String username) {
                //好友请求被同意
                contactListFragment.refresh();
            }

            @Override
            public void onContactRefused(String username) {
                //好友请求被拒绝
            }

            @Override
            public void onContactInvited(String username, String reason) {
                //收到好友邀请
            }

            @Override
            public void onContactDeleted(String username) {
                LogUtils.e("好友被删除了" + username);
                //被删除时回调此方法 刷新列表
                ContactInitialization();
            }
            @Override
            public void onContactAdded(String username) {
                LogUtils.e("添加好友了" + username);
                //增加了联系人时回调此方法 刷新列表
                ContactInitialization();
            }
        });
    }
    /**
     * 环信
     *
     * 联系人列表刷新
     */
    private void ContactInitialization() {
        new Thread() {//需要在子线程中调用
            @Override
            public void run() {
                //需要设置联系人列表才能启动fragment
                contactListFragment.setContactsMap(getContact());
                //刷新
                contactListFragment.refresh();
            }
        }.start();
    }

    /**
     * 环信
     *
     * 获取当前好友数量
     * @return
     */
    private Map<String, EaseUser> getContact() {
        Map<String, EaseUser> map = new HashMap<>();
        try {
            List<String> userNames = EMClient.getInstance().contactManager().getAllContactsFromServer();
            LogUtils.e("当前好友数量:" + userNames.size());
            for (String userId : userNames) {
                LogUtils.e("好友列表中有 : " + userId);
                map.put(userId, new EaseUser(userId));
            }
        } catch (HyphenateException e) {
            e.printStackTrace();
        }
        return map;
    }
}

